﻿namespace CatClient.Util
{
    using System;
    using System.Runtime.InteropServices;
    using System.Threading;

    public class MilliSecondTimer
    {
        private const int OneMinuteMs = 0xea60;
        private const long Ticks1970 = 0x89f7ff5f7b58000L;

        public static void SleepToNextMinute(int delay = 0)
        {
            DateTime now = DateTime.Now;
            Thread.Sleep((int) (((0xea60 - ((now.Second * 0x3e8) + now.Millisecond)) + 500) + delay));
        }

        public static long TicksFrom1970 =>
            (DateTime.Now.Ticks - 0x89f7ff5f7b58000L);

        public static long UtcTicksFrom1970 =>
            (DateTime.UtcNow.Ticks - 0x89f7ff5f7b58000L);

        public static long CurrentTimeMicros =>
            (DateTime.Now.Ticks / 10L);

        public static long CurrentTimeMillis =>
            (UtcTicksFrom1970 / 0x2710L);

        public static long CurrentTimeMinute =>
            (CurrentTimeMillis / 0xea60L);

        public static long CurrentTimeHoursForJava =>
            (CurrentTimeMillis / 0x36ee80L);
    }
}

